//
//  SLEmoticonPopView.swift
//  Emotion
//
//  Created by lirenqiang on 16/6/2.
//  Copyright © 2016年 lirenqiang. All rights reserved.
//

import UIKit

class SLEmoticonPopView: UIView {

    @IBOutlet weak var emoticonBtn: SLEmoticonButton!
    
    class func loadEmoticonPopView() -> SLEmoticonPopView {
        
        
        return NSBundle.mainBundle().loadNibNamed("SLEmoticonPopView", owner: nil, options: nil).last as! SLEmoticonPopView
    }
    
    func show(btn: SLEmoticonButton) {
        //拿到最后一个window
        let window = UIApplication.sharedApplication().windows.last
        if self.superview == nil {
            
            window?.addSubview(self)
        }
        
        self.hidden = false
        
        //转换坐标
        let rect = btn.convertRect(btn.bounds, toView: window)
        
        self.center.x = CGRectGetMidX(rect)
        self.frame.origin.y = CGRectGetMaxY(rect) - self.bounds.height
        self.emoticonBtn.emoticon = btn.emoticon
        
        
    }
    
    func dismissAfter() {
        
        
        //延后移除
        let time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.2))
        
        dispatch_after(time_t, dispatch_get_main_queue()) { 
            self.hidden = true
        }
        

        
    }

}
